package wordy

import (
	// "fmt"
	"regexp"
	"strconv"
	// "strings"
)

const testVersion = 1

var All = regexp.MustCompile(`What is -?[0-9]+ [plus|minus|multiplied by|divided by -?[0-9]+]?`)
var Operation = regexp.MustCompile(`plus|minus|multiplied by|divided by`)
var Num = regexp.MustCompile(`-?[0-9]+`)

func Answer(input string) (answer int, ok bool) {
	r := All.FindString(input)
	if len(r) == 0 {
		return
	}

	o := Operation.FindAllString(input, -1)
	n := Num.FindAllString(input, -1)
	sum, _ := strconv.Atoi(n[0])

	for i := 0; i < len(o); i++ {
		t, _ := strconv.Atoi(n[i+1])
		switch {
		case o[i] == "plus":
			sum += t
		case o[i] == "minus":
			sum -= t
		case o[i] == "multiplied by":
			sum = sum * t
		case o[i] == "divided by":
			sum = sum / t
		}
	}

	answer = sum
	ok = true
	return
}
